<template>
  <div class="password">
    <div class="com-container" v-if="showView">
      <div class="show-item">
        <span class="name">修改密码</span>
        <p class="btn-wrap">
          <span class="edit" @click="toEdit">修改</span>
        </p>
      </div>
    </div>
    <div class="com-form" v-else>
      <div class="postForm">
        <div class="form-item">
          <div class="text" required>原密码:</div>
          <div class="input">
            <input v-model="pwd" type="password" placeholder="请输入原密码" />
          </div>
        </div>
        <div class="form-item">
          <div class="text" required>新密码:</div>
          <div class="input">
            <input
              v-model="newPwd"
              type="password"
              placeholder="请输入新密码"
            />
          </div>
        </div>
        <div class="form-item">
          <div class="text" required>重复新密码:</div>
          <div class="input">
            <input
              v-model="secPwd"
              type="password"
              placeholder="请再输入新密码"
            />
          </div>
        </div>
        <div class="submit">
          <button class="confirm" type="button" @click="submit">保存</button>
          <button class="cancel" type="button" @click="cancel">取消</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { updatePass } from '@/api/users';
export default {
  props: ['password'],
  data () {
    return {
      showView: true,
      pwd: '',
      newPwd: '',
      secPwd: ''
    }
  },
  watch: {
  },
  created () {
  },
  mounted () {
  },
  methods: {
    toggleView () {
      this.showView = false;
    },
    toEdit () {
      this.showView = false;
    },
    cancel () {
      this.showView = true;
    },
    submit () {
      if (!this.pwd || !this.newPwd || !this.secPwd) {
        this.$message.error('请填写完整！')
        return
      }
      updatePass({
        old_password: this.pwd,
        new_password: this.newPwd,
        password: this.secPwd,
      }).then(res => {
        if (res.code === 200) {
          this.$message.success(res.msg || '提交成功！');
          this.showView = true;
        } else {
          this.$message.error(res.msg)
        }
      }).catch(err => {
        console.log(err)
        this.$message.error(err || '网络错误！')
      })
    },
  }
}
</script>
<style lang="scss" scoped>
.password {
  position: relative;
  .show-item {
    line-height: 70px;
    padding-left: 30px;
    border-bottom: 1px solid #ebebeb;
    & > span {
      margin-right: 30px;
      display: inline-block;
    }

    .btn-wrap {
      float: right;
      margin-right: 20px;
      span {
        margin-left: 20px;
      }
      .edit {
        color: $base-color;
        cursor: pointer;
        &:hover {
          color: $base-color-hover;
        }
      }
      .delete {
        color: $base-color-danger;
        cursor: pointer;
        &:hover {
          color: $base-color-important;
        }
      }
    }
  }
  .com-form {
    padding: 30px 0;
    .postForm {
      width: 500px;
      margin: 0 auto;
      .form-item {
        margin-bottom: 10px;
        line-height: 40px;
        & > div {
          display: inline-block;
        }
        & > .text {
          position: relative;
          padding-left: 20px;
          min-width: 120px;
          color: #24292f;
          vertical-align: top;
          &[required] {
            &::before {
              content: '*';
              position: absolute;
              left: 0px;
              top: 0px;
              color: $base-color-danger;
            }
          }
        }
        .input {
          width: 350px;
          input {
            line-height: 28px;
            border: 1px solid #ccc;
            border-radius: 4px;
            padding: 0 10px;
            width: 100%;
            font-size: 16px;
            &:active,
            &:focus {
              border-color: $base-color;
            }
          }
          &.small {
            width: 220px;
          }
        }
      }
      .submit {
        margin-top: 60px;
        text-align: center;
        button {
          height: 45px;
          width: 150px;
          margin: 0 20px;
          border: 1px solid $base-color;
          border-radius: 6px;
          font-size: 16px;
          &:hover {
            box-shadow: 0 0 6px 0 $base-color;
          }
        }
        .cancel {
          background: #fff;
          color: $base-color;
        }
        .confirm {
          background: $base-color;
          color: #fff;
        }
      }
    }
  }
}
</style>
